/*********************
 站点对话框层 - 设置昵称
**********************/
MPT.addObject('SetNickname', function(){

	var element_jQ;
	var nickname_jQ;
	var hint_jQ;
	var submit_jQ;
	var callBackFun;
	var privacyCode;

	var prompt_map = new Object();
	prompt_map[-1] = "昵称最长为12个字符，由中英文数字组成(中文占2个字符)！";
	prompt_map[-2] = "昵称不能为空！";
	prompt_map[-5] = "此昵称已被使用！";
	prompt_map[-99] = "服务器忙，请重试！";
	
	var preValidate = function(){
		hint_jQ.hide();
		
		var validate = false;
		
		var prompt = '';

		if (Verifier.empty(nickname_jQ.val())) {

		    prompt = prompt_map[-2];

		} else if (!Verifier.nickname(nickname_jQ.val())) {

		    prompt = prompt_map[-1];

        } else {
		
			prompt = '';
			validate = true;
		}
		
		hint_jQ.text(prompt);
		return validate;
	};
	
	var submit = function()
	{
		if(!preValidate()){
			hint_jQ.show();
		}else{
		
			jQuery.ajax({ 
				type:"POST",
				url : "/auth/checkAndSaveNickname",
				data : {
					'nickname' : nickname_jQ.val(),
					'privacyCode' : privacyCode
				},
				dataType : 'json',
				success: function(_response){
					if(_response['result'] == 1)
					{
						//data:{
						//	guideSwitch:true|false
						//}
						
						if(callBackFun)
						{
							callBackFun();
							callBackFun = function(){};
						}
						
						if(_response['data']['guide_switch'])
						{
							window.location.href = '/guide/guide1.html';
						}
						else if(_response['data']['back_url']){
							var hash = window.location.hash;
							window.location.href = _response['data']['back_url'] + hash;
						}
						else
						{
						    window.location.href = MPT.Config['url']['myspace'];
							Dialog.hide();
						}
					}
					
					if(_response['result'] == -99)
					{
						//data{
						//	nickname:-5[被占用]|-1[不符合规范]|-2[为空]|-99[服务器忙]
						//};
						
						for(var key in _response['data'])
						{
						    hint_jQ.text(prompt_map[_response['data'][key]]).show();
						}
					}
				}
			});
		}
	};
	
	this.setElm = function(_elm)
	{
		element_jQ = jQuery(_elm);
		nickname_jQ = jQuery(".e_nickname", _elm);
		hint_jQ = jQuery(".e_hint", _elm);
	    submit_jQ = jQuery(".e_submit", _elm);
		
		nickname_jQ.focus();

		submit_jQ.click(function() { submit(); });

		Util.KeyBoardMonitor(nickname_jQ, 13, function() { submit(); });
	};
	
	// 设置修改昵称校验码
	this.setPrivacyCode = function(_privacyCode)
	{
		privacyCode = _privacyCode;
	};
	
	this.setCallBackFun = function(_fun)
	{
		callBackFun = _fun;
	};
});
